package itheima.mocc.MyReflect3;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ConstructorDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//        method1();
//        method2();

//        method3();
        Class clazz = Class.forName("itheima.mocc.MyReflect3.Student");
        Student student= (Student) clazz.newInstance();
        System.out.println(student);
    }

    private static void method3() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class clazz = Class.forName("itheima.mocc.MyReflect3.Student");
        Constructor constructor = clazz.getConstructor();
        Student student = (Student) constructor.newInstance();
        System.out.println(student);
    }

    private static void method2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class clazz = Class.forName("itheima.mocc.MyReflect3.Student");

        Constructor constructor = clazz.getConstructor(String.class, int.class);

        Student student = (Student) constructor.newInstance("李皮皮", 22);
        System.out.println(student);
    }

    private static void method1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class clazz = Class.forName("itheima.mocc.MyReflect3.Student");

        Constructor constructor = clazz.getDeclaredConstructor(String.class);
        constructor.setAccessible(true);
        Student student = (Student) constructor.newInstance("张皮皮");
        System.out.println(student);
    }
}
